package com.thx.org.dao;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.thx.common.orm.PaginationDao;
import com.thx.common.util.AppContext;
import com.thx.common.util.DateUtil;
import com.thx.org.model.Department;

@Repository
public class DepartmentDao extends PaginationDao<Department> {

	public List<Department> findAllByComId(String comId) {
		String hql = "from Department where comId = ? and deleted = '0'";
		return createQuery(hql, StringUtils.strip(comId)).list();
	}

	public List<Department> getByParentId(String depId) {
		String hql = "from Department where comId = ? and deleted = '0'";
		List<Object> objs =new ArrayList<Object>();
		objs.add(AppContext.getCurrentComId());
		if (StringUtils.isBlank(depId)) {
			hql += " and parentId is null";
		} else {
			hql += " and parentId = ?";
			objs.add(StringUtils.strip(depId));
		}
		hql += " order by orderSeq";
		return createQuery(hql, objs.toArray()).list();
	}
	
	/**
	 * 根据父部门id获取子部门数量   
	 * @param depId
	 * @return
	 */
	public int getChildDeptNumByParentId(String depId) {
		String hql = "select count(*) from Department where comId = ? and deleted = '0'";
		List<Object> objs =new ArrayList<Object>();
		objs.add(AppContext.getCurrentComId());
		if (StringUtils.isBlank(depId)) {
			hql += " and parentId is null";
		} else {
			hql += " and parentId = ?";
			objs.add(StringUtils.strip(depId));
		}
		Number num = (Number)createQuery(hql, objs.toArray()).uniqueResult();
		return num.intValue();
	}

	public List<Department> getAllDeps() {
		String comId = AppContext.getCurrentComId();
		String hql = "from Department where comId = ? and deleted = '0'";
		return createQuery(hql, comId).list();
	}

	public Department findByName(String depName, String pid) {
		String hql = "from Department where comId = ? and deleted = '0'";
		List<Object> objs =new ArrayList<Object>();
		objs.add(AppContext.getCurrentComId());
		if (StringUtils.isNotBlank(depName)) {
			hql += " and depName = ?";
			objs.add(StringUtils.strip(depName));
		}
		if (StringUtils.isBlank(pid)) {
			hql += " and parentId is null";
		} else {
			hql += " and parentId = ?";
			objs.add(StringUtils.strip(pid));
		}
		List<Department> deps = createQuery(hql, objs.toArray()).list();
		if (deps != null && deps.size() > 0)
			return deps.get(0);
		return null;
	}

	public Department findByName(String depName) {
		String hql = "from Department where comId = ? and deleted = '0' and depName = ?";
		String comId = AppContext.getCurrentComId();
		List<Department> deps = createQuery(hql, comId, StringUtils.strip(depName)).list();
		if (deps != null && deps.size() > 0)
			return deps.get(0);
		return null;
	}

	public List<Department> getByParentId(String depId, int orderSeq) {
		String hql = "from Department where comId = ? and deleted = '0'";
		List<Object> objs =new ArrayList<Object>();
		objs.add(AppContext.getCurrentComId());
		if (StringUtils.isBlank(depId)) {
			hql += " and parentId is null";
		} else {
			hql += " and parentId = ?";
			objs.add(StringUtils.strip(depId));
		}
		hql += " and orderSeq >= ? order by orderSeq";
		objs.add(orderSeq);
		return createQuery(hql, objs.toArray()).list();
	}

	public Department findByFullName(String fullName) {
		String hql = "from Department where comId = ? and deleted = '0'";
		List<Object> objs =new ArrayList<Object>();
		objs.add(AppContext.getCurrentComId());
		if (StringUtils.isNotBlank(fullName)) {
			hql += " and fullName = ?";
			objs.add(StringUtils.strip(fullName));
		}
		List<Department> deps = createQuery(hql, objs.toArray()).list();
		if (deps != null && deps.size() > 0)
			return deps.get(0);
		return null;
	}

	public List<Department> getAllByLastUpdateTime(String lastUpdateTime) throws ParseException {
		String hql = "from Department where comId = ? and deleted = '0'";
		List<Object> objs =new ArrayList<Object>();
		objs.add(AppContext.getCurrentComId());
		if(StringUtils.isNotBlank(lastUpdateTime)){
			hql += " and changeTime > ?";
			Date last = DateUtil.parse(StringUtils.strip(lastUpdateTime), "yyyy-MM-dd HH:mm:ss");
			objs.add(last);
		}
		List<Department> deps = createQuery(hql, objs.toArray()).list();
		return deps;
	}
	
	/**
	 * 根据递归函数获取子部门及其本身集合
	 * @param parentId
	 * @return
	 */
	public  List<Department> getAllByParentId(String parentId){
		String sql = "select dep_id,dep_name,parent_id from org_department where FIND_IN_SET(dep_id,getChildList(?))";
	    List<Object[]> list = createSqlQuery(sql, new Object[]{parentId}).list();
	    if(list != null && list.size() > 0){
	    	 List<Department> deps = new ArrayList<Department>();
	    	for(Object[] o:list){
	    		Department p = new Department();
	    		p.setDepId((String) o[0]);
	    		p.setParentId((String) o[2]);
	    		p.setDepName((String) o[1]);
	    		deps.add(p);
	    	}
	    	return deps;
	    }
	   return null;
	}
	
	/**
	 * 
	 * @description 根据部门全路径查询该部门
	 * @author wjj
	 * @date 2014年12月18日
	 * Department
	 * @param depFullName
	 * @return
	 */
	public Department getDepByDepFullName(String depFullName){
		String hql = "from Department where fullName = ? and comId = ? and deleted = '0'";
		@SuppressWarnings("unchecked")
		List<Department> list = createQuery(hql, StringUtils.strip(depFullName), AppContext.getCurrentComId()).list();
		if(list != null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}
}
